package com.nopay.cdxphone.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nopay.cdxphone.bean.response.VIPResponse;

import java.util.ArrayList;
import java.util.List;

import com.nopay.cdxphone.R;

public class VIPAdapter extends BaseAdapter {

    private List<VIPResponse.Data> datas = new ArrayList();
    private LayoutInflater mInflater;

    public VIPAdapter(Context context) {
        super();
        mInflater = LayoutInflater.from(context);
    }

    public void addList(List<VIPResponse.Data> datas) {
        if(datas == null) return;

        this.datas.addAll(datas);
        notifyDataSetChanged();
    }

    public void clear() {
        this.datas.clear();
        notifyDataSetChanged();
    }

    public VIPResponse.Data getSelectedVIP() {
        for (int i = 0; i < getCount(); i++) {
            if(getItem(i).isSelected()) return getItem(i);
        }
        return null;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public VIPResponse.Data getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if(convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.item_vip, null);
            holder.layout_vip = convertView.findViewById(R.id.layout_vip);
            holder.tvMemberLevel = convertView.findViewById(R.id.tv_member_level);
            holder.tvMonthNum = convertView.findViewById(R.id.tv_month_num);
            holder.tvPrice = convertView.findViewById(R.id.tv_price);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        VIPResponse.Data bean = getItem(position);
        holder.layout_vip.setBackgroundResource(bean.isSelected() ? R.drawable.shape_bg_vip_item_selected : R.drawable.shape_bg_vip_item_normal);
        holder.tvMemberLevel.setText(bean.getMemberLevel());
        holder.tvMonthNum.setText("有效期" + bean.getDays()  + "天");
        holder.tvPrice.setText(bean.getPriceByFormat2() + "");

        return convertView;
    }

    public class ViewHolder {
        private LinearLayout layout_vip;
        private TextView tvMemberLevel;
        private TextView tvMonthNum;
        private TextView tvPrice;
    }
}
